package com.meihua.baoleyi.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.meihua.baoleyi.R;
import com.meihua.baoleyi.adapters.AddressMessageAdapter;
import com.meihua.baoleyi.base.BaseActivity;
import com.meihua.baoleyi.bean.AddressListBean;
import com.meihua.baoleyi.common.APIServer;
import com.meihua.baoleyi.common.MyApp;
import com.meihua.baoleyi.net.NetHelper;
import com.meihua.baoleyi.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by liming on 2017/10/17.
 * 地址管理
 */

public class AddressActivity extends BaseActivity implements View.OnClickListener {

    private ListView lv_address_manager;//地址管理listview
    private AddressMessageAdapter adapter;//适配器
    private TextView text_title;//标题
    private ImageView text_back;//返回
    private TextView top_right_tv;//右侧操作显示
    private List<AddressListBean.DatasBean> result = new ArrayList<>();
    private Button mBt_address;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address);
        initView();
    }
    //初始化数据
    private void initView() {
        lv_address_manager = (ListView) findViewById(R.id.lv_address_manager);
        text_title = (TextView) findViewById(R.id.text_title);
        text_back = (ImageView) findViewById(R.id.text_back);
        top_right_tv = (TextView) findViewById(R.id.top_right_tv);
        mBt_address = findViewById(R.id.bt_add_address);

        adapter = new AddressMessageAdapter(this, result);
        lv_address_manager.setAdapter(adapter);
        text_title.setText("地址管理");
        text_back.setOnClickListener(this);
        top_right_tv.setOnClickListener(this);
        mBt_address.setOnClickListener(this);

        getServerData();
    }

    /**
     * 获取地址信息
     */
    public void getServerData() {
        showDialog();
        TreeMap<String, String> treeMap = new TreeMap<String, String>();
        treeMap.put("user_id", MyApp.getInstance().getUser_id());
        NetHelper.get(APIServer.ADDRESSLIST, treeMap, true, new NetHelper.HttpCallBack() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                AddressListBean bean = NetHelper.getBeanForJson(AddressListBean.class, s);
                if (bean == null) return;
                if(bean.getCode().equals("200")) {
                    result.clear();
                    result.addAll(bean.getDatas());
                    adapter.notifyDataSetChanged();
                } else {
                    result.clear();
                    adapter.notifyDataSetChanged();
                }
                dismissDialog();
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.text_back://返回
                finish();
                break;
            case R.id.bt_add_address:
                /*添加地址*/
                Intent in = new Intent(AddressActivity.this, AddAdressActivity.class);
                startActivityForResult(in, 1);
                break;
        }
    }

    /**
     * 页面返回的回调
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //添加地址成功
        if (requestCode == 1 && resultCode == RESULT_OK) {
            getServerData();
        }
        //修改地址成功
        if (requestCode == 2 && resultCode == RESULT_OK) {
            getServerData();
        }
    }
}
